package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import com.itheima.reggie.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper,Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;  //套餐菜品表
    //--1--添加----操作两个表
    @Override
    @Transactional
    public void saveWithsetmealDish(SetmealVO setmealVO) {
        log.info("[>>>添加套餐--setmealVO]：{}",setmealVO);
        //-T1.添加--setmeal
        this.save(setmealVO);
        Long setmealId = setmealVO.getId();

        //-T2.添加--setmealDish
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        setmealDishes=setmealDishes.stream().map(item->{
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }


    //--2--删除----操作两个表
    //--ids:  Setmeal的id
    @Override
    @Transactional
    public String  deleteWithSetmealDish(List<Long> ids) {
        //-T1.删除套餐----是否处于售卖状态，如果是，不允许删除

        //查到停售套餐集合
        List<Setmeal> setmealList= this.list(
                new LambdaQueryWrapper<Setmeal>().eq(Setmeal::getStatus, 0).in(Setmeal::getId, ids));

        //可删除id集合
        List<Long> ids_new=new ArrayList<>();
        for (Setmeal setmeal : setmealList) {
            ids_new.add(setmeal.getId());
        }
        log.info("[>>>删除套餐--可删除id集合]: {}",ids_new);


        if (ids_new.size()==0){
            return "当前无停售套餐可删除";
        }

        this.removeByIds(ids_new); //物理删除--停售的套餐
        //-T2.删除套餐对应的菜品数据：  物理删除
        setmealDishService.remove(new LambdaQueryWrapper<SetmealDish>().in(SetmealDish::getSetmealId,ids_new));
        return "套餐删除成功!!";
    }

    //起售、停售

    @Override
    public void upSetmeal(List<Long> ids, Integer status) {
        this.update(new LambdaUpdateWrapper<Setmeal>().set(Setmeal::getStatus,status).in(Setmeal::getId,ids));
    }

    //--修改
    @Override
    @Transactional
    public void updateWithsetmealDish(SetmealVO setmealVO) {

        //--1--  修改--覆盖套餐的数据
        this.updateById(setmealVO);

        //--2--  删除--覆盖套餐dish的数据
        setmealDishService.remove(new LambdaQueryWrapper<SetmealDish>().in(SetmealDish::getSetmealId,setmealVO.getId()));

        //--3--  修改--覆盖套餐dish的数据---需要遍历，以防新加入数据没有`套餐id`
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        setmealDishes=setmealDishes.stream().map(item->{
            item.setSetmealId(setmealVO.getId());
            return item;
        }).collect(Collectors.toList());

        //--3-2--  修改--覆盖套餐dish的数据---需要遍历，以防新加入数据没有`套餐id`
        setmealDishService.saveBatch(setmealDishes);
    }
}
